<template>
  <div class="erap"></div>
</template>
<script>
import { getUserInfo, login } from "@/utils/interface";
export default {
  created() {
    if (this.getUrlParams("code")) {
      //如果连接中能拿到code说明此时access_token没有或者已过期，需要重新获取token
      let obj = {};
      getUserInfo({ code: this.getUrlParams("code") }).then(res => {
        if (res.data.code == 200 && res.data.data.openid) {
          localStorage.setItem("wechataccess_user_token", res.data.data.openid);
          localStorage.setItem("mobileInfo", JSON.stringify(res.data.data));
          login({
            openId: localStorage.getItem("wechataccess_user_token")
          }).then(res => {
            if (res.data.code == 200) {
              localStorage.setItem("customerwx", res.data.data.jsessionid);
              if (
                this.getUrlParams("state") &&
                this.getUrlParams("state") != "STATE"
              ) {
                window.location.href =
                  "http://www.jiebaodingdan.com/customer-mobile?state=" +
                  this.getUrlParams("state");
              } else {
                window.location.href =
                  "http://www.jiebaodingdan.com/customer-mobile/#/";
              }
            } else {
              if (
                this.getUrlParams("state") &&
                this.getUrlParams("state") != "STATE"
              ) {
                window.location.href =
                  "http://www.jiebaodingdan.com/customer-mobile?state=" +
                  this.getUrlParams("state") +
                  "/#/register";
              } else {
                window.location.href =
                  "http://www.jiebaodingdan.com/customer-mobile/#/register";
              }
            }
          });
          //跳转
        } else {
          if (
            this.getUrlParams("state") &&
            this.getUrlParams("state") != "STATE"
          ) {
            window.location.href =
              "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx88c9827af713656d&redirect_uri=http%3A%2F%2Fwww.jiebaodingdan.com%2Fcustomer-mobile%2F%23%2Fauth&response_type=code&scope=snsapi_userinfo&state=" +
              this.getUrlParams("state") +
              "#wechat_redirect";
          } else {
            window.location.href =
              "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx88c9827af713656d&redirect_uri=http%3A%2F%2Fwww.jiebaodingdan.com%2Fcustomer-mobile%2F%23%2Fauth&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
          }
        }
      });
    } else {
      if (this.getUrlParams("state") && this.getUrlParams("state") != "STATE") {
        window.location.href =
          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx88c9827af713656d&redirect_uri=http%3A%2F%2Fwww.jiebaodingdan.com%2Fcustomer-mobile%2F%23%2Fauth&response_type=code&scope=snsapi_userinfo&state=" +
          this.getUrlParams("state") +
          "#wechat_redirect";
      } else {
        window.location.href =
          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx88c9827af713656d&redirect_uri=http%3A%2F%2Fwww.jiebaodingdan.com%2Fcustomer-mobile%2F%23%2Fauth&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
      }
    }
  },
  methods: {
    getUrlParams(name) {
      // 不传name返回所有值，否则返回对应值
      var url = window.location.search;
      if (url.indexOf("?") == 1) {
        return false;
      }
      url = url.substr(1);
      url = url.split("&");
      var name = name || "";
      var nameres;
      // 获取全部参数及其值
      for (var i = 0; i < url.length; i++) {
        var info = url[i].split("=");
        var obj = {};
        obj[info[0]] = decodeURI(info[1]);
        url[i] = obj;
      }
      // 如果传入一个参数名称，就匹配其值
      if (name) {
        for (var i = 0; i < url.length; i++) {
          for (const key in url[i]) {
            if (key == name) {
              nameres = url[i][key];
            }
          }
        }
      } else {
        nameres = url;
      }
      // 返回结果
      return nameres;
    }
  }
};
</script>